package backtracking;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 海琳琦
 * @Date: 2022/2/26 14:28
 * https://leetcode-cn.com/problems/subsets-ii/
 * 子集 II
 */
public class SubsetsWithDup {

    static List<List<Integer>> result;
    static LinkedList<Integer> list;

    public List<List<Integer>> subsetsWithDup(int[] nums) {
        result = new ArrayList<>();
        list = new LinkedList<>();
        Arrays.sort(nums);
        backTracking(nums, 0);
        return result;
    }

    private void backTracking(int[] nums, int start) {
        result.add(new ArrayList<>(list));
        for (int i = start; i < nums.length; i++) {
            list.add(nums[i]);
            backTracking(nums, i + 1);
            list.removeLast();
            while (i != nums.length - 1 && nums[i] == nums[i + 1]) {
                i++;
            }
        }
    }


    public static void main(String[] args) {

    }
}
